# Maintainer : fkxxyz <fkxxyz@gmail.com>

pkgname=ccsm-standalone
pkgver="0.8.18"
pkgrel=1
pkgdesc="Compizconfig Settings Manager"
arch=('any')
url="https://gitlab.com/compiz/ccsm/"
license=('GPL')
depends=('libcompizconfig' 'gtk2' 'glib2' 'python' 'cairo' 'gobject-introspection-runtime' 'libxrandr' 'libsm' 'libxdamage')
makedepends=('intltool' 'pkgconfig')
groups=('compiz-fusion')
conflicts=('ccsm-git' 'ccsm' 'compizconfig-python' 'compizconfig-python-git')
provides=("ccsm=$pkgver")
source=(
	"${url}-/archive/v${pkgver}/ccsm-v${pkgver}.tar.bz2"
	"https://gitlab.com/compiz/compizconfig-python/-/archive/v${pkgver}/compizconfig-python-v${pkgver}.tar.bz2"
)

build() {
	cd "${srcdir}"

  # create venv
  python -m venv --clear --upgrade-deps "${srcdir}/venv"

  # activate venv
  source "${srcdir}/venv/bin/activate"

  # install python depends
  pip install setuptools
  pip install Cython
  pip install pycairo
  pip install PyGObject==3.44.1

  # compile compizconfig
	cd "${srcdir}/compizconfig-python-v${pkgver}"
	NOCONFIGURE=1 ./autogen.sh
  python_includes="$(python-config --includes)"
	PYTHON=python CFLAGS="$python_includes" ./configure --prefix=/opt/ccsm
	make
}

package() {
  # activate venv
  source "${srcdir}/venv/bin/activate"

  # package python
  install -d "${pkgdir}/opt/ccsm"
  cp -rpT "${srcdir}/venv" "${pkgdir}/opt/ccsm"

  # package compizconfig
	cd "${srcdir}/compizconfig-python-v${pkgver}"
	make DESTDIR="${pkgdir}" install

  # package ccsm
  cd "${srcdir}/ccsm-v${pkgver}"
  python ./setup.py install --prefix=/opt/ccsm --install-data="/usr" --with-gtk=2.0 --root="${pkgdir}"

  # make ccsm shortcut
  ln -s /usr/share "${pkgdir}/opt/ccsm/share"
  PYTHONPATH="/opt/ccsm$(python -c "import sys; import re; print(re.sub(r'^.*((/[^/]+){3})', '\\\\1', sys.path[-1]))")"
  sed -i '1c#!/usr/bin/python' "${pkgdir}/opt/ccsm/bin/ccsm"
  printf '#!/bin/bash\nPYTHONPATH="%s" exec /opt/ccsm/bin/ccsm' "$PYTHONPATH" | install -Dm755 /dev/stdin "${pkgdir}/usr/bin/ccsm"

  # strip python
  cd "${pkgdir}/opt/ccsm"
  bin/python bin/pip uninstall -y setuptools Cython pip
  find . -type d -name __pycache__ | xargs rm -rf
  rm -rf pyvenv.cfg include
  cd bin
  ls -1 . | grep -v ccsm | xargs rm -rf
}

sha256sums=('0dda29684501fee692fa90f3af29503872dd7c9f6b28353f7ba22e4436ce17f8'
            '2add2083365eee2f966db826c3967f030d949b2b5aad59e738ba3b782587ad93')

